package com.zhang.springcloud.controller;

import com.netflix.discovery.converters.Auto;
import com.zhang.springcloud.entities.CommonResult;
import com.zhang.springcloud.entities.Payment;
import com.zhang.springcloud.lb.LoadBanance;
import com.zhang.springcloud.lb.LoadBananceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
public class OrderController {
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBanance loadBanance;
    @Value("${server.port}")
    private String serverPort;
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/add",payment,CommonResult.class);
    }
    @GetMapping("/consumer/payment/create1")
    public CommonResult<Payment> create1(Payment payment){
       ResponseEntity<CommonResult> rs= restTemplate.postForEntity(PAYMENT_URL+"/payment/add",payment,CommonResult.class);
        return rs.getBody();
    }

    @GetMapping("/consumer/payment/select/{id}")
    public CommonResult<Payment> select(@PathVariable("id") Long id){
//        restTemplate.put();
//        restTemplate.delete();
        return restTemplate.getForObject(PAYMENT_URL+"/payment/select/"+id,CommonResult.class);
    }
    @GetMapping("/consumer/payment/select1/{id}")
    public CommonResult<Payment> select1(@PathVariable("id") Long id){
        //用entities可以获取请求头信息等
        ResponseEntity<CommonResult> rs= restTemplate.getForEntity(PAYMENT_URL+"/payment/select/"+id,CommonResult.class);
        //http请求 200 成功  http请求信息
        if(rs.getStatusCode().is2xxSuccessful()){
            return rs.getBody();
        }else{
            return new CommonResult<>(444,"请求失败",null);
        }

    }

    //自定义
    @GetMapping("/consumer/payment/lb")
    public String getLb(){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if(instances == null && instances.size() <=0){
            return null;
        }
        ServiceInstance serviceInstance = loadBanance.instance(instances);
        URI url = serviceInstance.getUri();
        return restTemplate.getForObject(url+"/payment/lb",String.class);
    }
    @GetMapping(value = "/payment/lb")
    public String getPaymentLB() {
        return serverPort;
    }
    @GetMapping("/consumer/order/zipkin")
    public String zipkin(){
        return restTemplate.getForObject(PAYMENT_URL+"/zipkin",String.class);
    }
}




















